function [val b] = NegaMax(board, depth, alpha, beta)
  
    if depth == 0
        val = evalfunc(board,1);
        return
    end

% Get the list of plausable moves, sorted by most pieces flipped
    moves = GetValidFieldsNoValues( board , 1 );% weighted matrix!
    [row,column,value]=find(moves);
    PosMat=sortrows([row,column,value],-3);% Sortieren!
    row=PosMat(:,1);
    column=PosMat(:,2);
    % value=PosMat(:,3);
    numPosMoves = length(row);

    if numPosMoves == 0   % no posibble moves
        if nargout == 2     
            b = board;      
            val = 0;          
            return;
        end
        % Gegner am Zug -> Werte negieren; alpha und beta vertauschen
        val = -NegaMax(-board, depth-1, -beta, -alpha);
        return;
    end
    
    
    for i=1:numPosMoves
        B=make_move(board,row(i),column(i),1);

        % Gegner am Zug -> Werte negieren; alpha und beta vertauschen
        currentValue= -NegaMax(-B, depth-1, -beta, -alpha);  
        if (currentValue>= beta)
            val = beta;       
        return          % beta-cut
        end
        if (currentValue> alpha)
            alpha = currentValue;   % best Move
            bestPosIdx = i;
        end
    end
    
    val = alpha; 

    if nargout == 2 
    b = make_move(board,row(bestPosIdx),column(bestPosIdx),1);  
    end
    
    
    
end